package com.itheima.controller;
import cn.hutool.crypto.digest.DigestUtil;
import com.itheima.common.Code;
import com.itheima.common.Result;
import com.itheima.exception.TokenException;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.vo.EmployeeVo;
import com.itheima.vo.PagingQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;


/**
 * @author :WuJunJie
 * packageName :com.itheima.controller
 * fileName :EmployeeController
 * creationTime :2022-11-20 17:22
 */
@RestController
@RequestMapping("employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("login")
    public Result login(@RequestBody Employee employee){
        String md5Hex1 = DigestUtil.md5Hex(employee.getPassword());
        EmployeeVo employeeVo = employeeService.findByUsernameAndPassword(employee.getUsername(), md5Hex1);
        if (employeeVo==null){
            return Result.error(Code.NotFound.getCode());
        }
            return Result.success(employeeVo);
    }


    @PostMapping("logout")
    public Result logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if (redisTemplate.opsForValue().get("employee_token:"+token)==null){
            return Result.error(Code.Forbidden.getCode());
        }

        redisTemplate.delete("msg" + token);
        return Result.success();
    }

    @PostMapping("")
    public Result insertEmployee(@RequestBody Employee employee){
        employee.setPassword(DigestUtil.md5Hex(employee.getPassword()));
        Boolean res = employeeService.insertEmployee(employee);
        if(res){
            return Result.success("新增成功");
        }
        return Result.error(Code.Created.getCode());
    }

    @PutMapping("")
    public Result updateEmployee(@RequestBody Employee employee){
        Boolean update=employeeService.updateEmployee(employee);
        if (update) {
            return Result.success("员工信息修改成功");
        }
        return Result.error(Code.Created.getCode());

    }

    @GetMapping("{id}")
    public Result selectById( @PathVariable String id){

        EmployeeVo employeeVo=employeeService.selectById(id);
        log.info("{}",employeeVo);
        return Result.success(employeeVo);
    }


    @GetMapping("page")
    public Result selectPage( @RequestParam(name="name",required = false) String name,
                              @RequestParam(name = "page",defaultValue = "1")Integer page,
                              @RequestParam(name = "pageSize",defaultValue = "2") Integer pageSize){
        PagingQueryVo<EmployeeVo> employeeVoPagingQueryVo = employeeService.selectPage(name, page, pageSize);


        return Result.success(employeeVoPagingQueryVo);
    }



}
